package jp.isisredirect.DreamWaverFirst;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

import java.io.File;

//import android.view.View;

public class AutoController extends AccessoryController {

	//private boolean mVertical;

	class ArrayAdapterDeamWaverStoreData extends ArrayAdapter<DreamWaverStoreData> {
		private LayoutInflater m_Inflater = null;
		private DreamWaverStoreData[] m_Items = null;
		
		public ArrayAdapterDeamWaverStoreData(Context context, int resource, int textViewResourceId,  
				DreamWaverStoreData[] items) {
	        super(context, resource, textViewResourceId, items);
	        m_Inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	        m_Items = items;
	    }
		
		public View getView(int position, View convertView, ViewGroup parent) {

			View view = convertView;
			if (view == null) {
				view = m_Inflater.inflate(R.layout.rowitem, null);
			}
			if (m_Items != null && m_Items[position] != null) {
				OnClickRow click_event = new OnClickRow(position, view);
				view.setOnClickListener(click_event);

				TextView textview = (TextView) view
						.findViewById(R.id.row_textview1);
				textview.setOnClickListener(click_event);
				textview.setText(m_Items[position].title);
				OnClickTitle clicktitleevent = new OnClickTitle(position,textview);
				textview.setOnClickListener(clicktitleevent);

				CheckBox checkbox = (CheckBox) view.findViewById(R.id.checkBox);
				checkbox.setOnClickListener(click_event);
				checkbox.setChecked(m_Items[position].checked);
			}
			return view;
		}
		
		private class OnClickTitle implements OnClickListener {
			private int m_Position = 0;
			private View m_View = null;

			OnClickTitle(int position, View view) {
				m_Position = position;
				m_View = view;
			}

			public void onClick(View v) {
				mHostActivity.mPlayer.setStoreData(m_Items[m_Position]);
				mHostActivity.showTabContents(3);
			}
		}

		private class OnClickRow implements OnClickListener {
			private int m_Position = 0;
			private View m_RowView = null;

			OnClickRow(int position, View row_view) {
				m_Position = position;
				m_RowView = row_view;
			}

			public void onClick(View v) {
				CheckBox ck = (CheckBox)m_RowView.findViewById(R.id.checkBox);				
				m_Items[m_Position].checked = ck.isChecked();
				//TextView textview = (TextView)m_RowView.findViewById(R.id.row_textview1);
				//textview.setText(m_Items[m_Position].title + m_Items[m_Position].id);
			}
		}
	}
	
	public class DeleteButtonController implements OnClickListener {
		private DreamWaverStoreData[] data = null;
		private Button mButton;

		DeleteButtonController(DreamWaverStoreData[] d ) {
			data = d;
		}

		public void attachToView(View targetView) {
			mButton = (Button) targetView;
			mButton.setOnClickListener(this);
		}

		public void onClick(View b) {
			mHostActivity.mDBStore.open();
			boolean changed = false;
			int c = data.length;
			for (int i = 0; i < c; i++) {
				if (data[i] == null) {
					break;
				}
				if (data[i].checked) {
					File f = new File(data[i].filepath);
					if (f != null) {
						f.delete();
					}
					mHostActivity.mDBStore.delete(data[i].id);
					changed = true;
				}				
			}
			mHostActivity.mDBStore.close();
			if (changed) {
				updateUI();
			}
		}		
	}
	AutoController(DreamWaverFirstActivity hostActivity, boolean vertical) {
		super(hostActivity);
		//mVertical = vertical;
	}

	protected void onAccesssoryAttached() {
		updateUI();
	}

	private void updateUI() {
		//MainStore store;
		mHostActivity.mDBStore.open();
		DreamWaverStoreData[] data = null;
		data = mHostActivity.mDBStore.loadAll();
		mHostActivity.mDBStore.close();
		
		ListView list = (ListView) findViewById(R.id.ListView01);
		ArrayAdapterDeamWaverStoreData arrayAdapter
		 = new ArrayAdapterDeamWaverStoreData(mHostActivity, R.layout.rowitem, R.id.row_textview1, data);
		list.setAdapter(arrayAdapter);
		
		Button deletebutton = (Button) findViewById(R.id.delete);
		DeleteButtonController deletebuttoncontroller = new DeleteButtonController(data);
		deletebuttoncontroller.attachToView(deletebutton);
	}
	public void resetControls() {	
		updateUI();
	}

}
